package controllers;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import models.Resource;
import models.Setting;
import play.data.Form;
import play.mvc.Controller;
import play.mvc.Result;
import services.ConfigurationService;
import services.ProjectService;
import services.SettingService;
import services.TestCaseService;
import services.TestSuiteService;
import views.html.testsuites.*;

public class TestSuites extends Controller {
	
	private static final SettingService settingService = SettingService.getService();
	private static final ProjectService projectService = ProjectService.getService();
	private static final ConfigurationService configurationService = ConfigurationService.getService();
	private static final TestSuiteService testSuiteService = TestSuiteService.getService();
	private static final TestCaseService testCaseService = TestCaseService.getService();
	
	public static final String TEST_SUITE_FILE_EXTENSION = ".html";
	
	public static Result listTestSuites(long projectId) {
		List<Resource> testSuites = testSuiteService.getAllTestSuites(projectId);
		return ok(list.render(projectId, testSuites));
	}
	
	public static Result addTestSuite(long projectId) {
		Form<Resource> form = new Form<Resource>(Resource.class);
		List<Resource> testSuites = testSuiteService.getAllTestSuites(projectId);
		return ok(add.render(form, projectId, testSuites));
	}
	
	public static Result editTestSuite(long projectId, long testSuiteId) {
		Resource testSuite = testSuiteService.getTestSuite(testSuiteId);
		if (testSuite != null) {
			Form<Resource> form = Form.form(Resource.class).fill(testSuite);
			List<Resource> testCases = testCaseService.getAllTestCases(testSuiteId);
			return ok(edit.render(testSuiteId, form, projectId, testCases));
		} else {
			return redirect(controllers.routes.TestSuites.listTestSuites(projectId));
		}
	}
	
	public static Result createTestSuite(long projectId) {
		Resource testSuite = Form.form(Resource.class).bindFromRequest().get();
		testSuiteService.saveTestSuite(projectId, testSuite);
		return redirect(controllers.routes.TestSuites.editTestSuite(projectId, testSuite.getId()));
	}
	
	public static Result updateTestSuite(long projectId, long testSuiteId) {
		Resource testSuite = testSuiteService.getTestSuite(testSuiteId);
		if (testSuite != null) {
			Resource newTestSuite = Form.form(Resource.class).bindFromRequest().get();
			testSuite.setName(newTestSuite.getName());
			testSuite.setDescription(newTestSuite.getDescription());
			testSuiteService.saveTestSuite(projectId, testSuite);
			return redirect(controllers.routes.TestSuites.editTestSuite(projectId, testSuiteId));
		} else {
			return redirect(controllers.routes.TestSuites.listTestSuites(projectId));
		}
	}
	
	public static Result runTestsWithDefaultConfig(long projectId, long testSuiteId) {
		Resource defaultConfig = projectService.getDefaultConfig(projectId);
		return runTestsWithConfig(projectId, testSuiteId, defaultConfig);
	}
	
	public static Result runTestsWithConfig(long projectId, long testSuiteId, long configId) {
		Resource config = configurationService.getConfig(configId);
		return runTestsWithConfig(projectId, testSuiteId, config);
	}
	
	private static Result runTestsWithConfig(long projectId, long testSuiteId, Resource config) {
		Resource project = projectService.getProject(projectId);
		Resource testSuite = testSuiteService.getTestSuite(testSuiteId);
		if (project == null || testSuite == null || config == null) {
			return redirect(controllers.routes.TestSuites.listTestSuites(projectId));
		}
		String content = "";
		try {
			String testSuiteName = testSuite.getName();
			if (testSuiteName.endsWith(TEST_SUITE_FILE_EXTENSION)) {
				testSuiteName = testSuiteName.substring(0, testSuiteName.length() - TEST_SUITE_FILE_EXTENSION.length());
			}
			URL url = new URL(settingService.getSetting(Setting.TEST_EXECUTION_URL).getStringValue() + "/cgi-bin/console/execute.py?projectid=" + project.getName() + "&suite=" + testSuiteName + "&variablefile=" + config.getName());
			
			URLConnection connection = url.openConnection();
			BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				content += line + "\n";
			}
			in.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ok(content);
	}
	
}
